package com.kaifamiao.pipe;

import java.io.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 1、管道流
 *    PipedInputStream 、PipedOutputStream
 *    PipedReader、PipedWriter
 * 2、管道通道
 *    Pipe.SourceChannel 、Pipe.SinkChannel
 */
public class PipeTest {

    public static void main(String[] args) throws IOException {

        /*
        PipedOutputStream out = new PipedOutputStream();
        PipedInputStream in = new PipedInputStream();
        out.connect(in);
        */
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader();
        writer.connect(reader);

        BufferedReader br = new BufferedReader(reader);
        BufferedWriter bw = new BufferedWriter(writer);

        ExecutorService es = Executors.newFixedThreadPool(2);

        Callable writeTask = () -> {
            bw.write( "村里有个姑娘叫小芳" );
            bw.newLine();
            bw.flush();
            return null;
        };

        Callable readTask = () -> {
            String message = br.readLine();
            System.out.println( message );
            return null;
        };

        es.submit( writeTask );
        es.submit( readTask );

        es.shutdown();
    }

}
